import Box2 from "./Box2";

/**
 * 掉落盒子脚本，实现盒子碰撞及回收流程
 */
export default class Box1 extends Laya.Script {
    /**刚体对象引用 */
    private _rig: Laya.RigidBody;

    /** @prop {name:nextLevelBox,tips:"掉落容器预制体对象1",type:Prefab}*/
    nextLevelBox: Laya.Prefab;

    constructor() { 
        super();
        loadBox();
    }

    onEnable(): void {
        /**获得组件引用，避免每次获取组件带来不必要的查询开销 */
        this._rig = this.owner.getComponent(Laya.RigidBody);
    }

    onUpdate(): void {
       
    }

    loadBox(id : number):void{
        var owner: Laya.Sprite = this.owner as Laya.Sprite;
        owner.loadImage("test/box1.png");
    }

    onTriggerEnter(other: any, self: any, contact: any): void {
        // var owner: Laya.Sprite = this.owner as Laya.Sprite;
        // if (other.label === "box1") {
        //     Laya.SoundManager.playSound("sound/hit.wav");
        //     if (owner.parent) {
        //         let effect: Laya.Animation = Laya.Pool.getItemByCreateFun("effect", this.createEffect, this);
        //         effect.pos(owner.x, owner.y);
        //         owner.parent.addChild(effect);
        //         effect.play(0, true);

        //         owner.removeSelf();

                

        //         // /* 创建下一等级盒子 并设置类型为刚体类型（默认为静态类型） */
        //         // let _nextLevelBox : Laya.Sprite = Laya.Pool.getItemByCreateFun("nextLevelBox", this.nextLevelBox.create, this.nextLevelBox);
        //         // _nextLevelBox.pos(owner.x, owner.y - 10);
        //         // let _rig:Laya.RigidBody;
        //         // _rig = _nextLevelBox.getComponent(Laya.RigidBody);
        //         // _rig.type = "dynamic";
        //         // owner.parent.addChild(_nextLevelBox);

        //         Laya.SoundManager.playSound("sound/destroy.wav");
        //         }
        //     }
    }

    /**使用对象池创建爆炸动画 */
    createEffect(): Laya.Animation {
        let ani: Laya.Animation = new Laya.Animation();
        ani.loadAnimation("test/TestAni.ani");
        ani.on(Laya.Event.COMPLETE, null, recover);
        function recover(): void {
            ani.removeSelf();
            Laya.Pool.recover("effect", ani);
        }
        return ani;
    }

    onDisable(): void {
        //盒子被移除时，回收盒子到对象池，方便下次复用，减少对象创建开销。
        Laya.Pool.recover("box1", this.owner);
    }
}